from ast import For
from fastapi import FastAPI, Header,Form,Request
from fastapi.responses import JSONResponse ,HTMLResponse,RedirectResponse
from starlette.responses import FileResponse
from fastapi.templating import Jinja2Templates
from tortoise.contrib.fastapi import register_tortoise
import uuid
import os
app = FastAPI()


template=Jinja2Templates("../frontend")

@app.get("/")
def user(req:Request):
    return template.TemplateResponse("test.html",context={"request":req})


import base64
from fastapi import Body

@app.post("/base64file")
async def uploadfile(image=Body(None), suffix=Body(None)):
    imgdata = base64.b64decode(image)
    file_name = os.getcwd() + "/images/" + str(uuid.uuid1()) + "." + suffix
    file = open(file_name, 'wb')
    file.write(imgdata)
    file.close()
    return {"code": 0, "obj": file_name}

# if __name__ == '__main__':
#     Popen(['python', '-m', 'https_redirect'])  # Add this
#     uvicorn.run(
#         'main:app', port=443, host='0.0.0.0',
#         reload=True, reload_dirs=['html_files'],
#         ssl_keyfile='/etc/letsencrypt/live/my_domain/privkey.pem',
#         ssl_certfile='/etc/letsencrypt/live/my_domain/fullchain.pem')
